package com.qinghe.pro.service.article.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qinghe.pro.entity.article.Article;
import com.qinghe.pro.mapper.article.ArticleMapper;
import com.qinghe.pro.service.article.ArticleService;
import com.qinghe.utils.Page;

@Service("articleServiceImpl")
public class ArticleServiceImpl implements ArticleService {

	@Autowired
	private ArticleMapper articleMapper;

	@Override
	public Article get(Integer id) {
		return articleMapper.selectById(id);
	}

	@Override
	public void insert(Article article) {
		articleMapper.insertSelective(article);
	}

	@Override
	public void update(Article article) {
		articleMapper.updateByPrimaryKeySelective(article);
	}

	@Override
	public void delete(Integer id) {
		articleMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Article> findByPage(Article article, Page page) {
		PageHelper.startPage(page.getPageNum(), page.getNumPerPage()).setOrderBy("create_time desc");
		List<Article> list = articleMapper.selectAll(article);
		PageInfo<Article> p = new PageInfo<Article>(list);
		page.setTotalCount((int) p.getTotal());
		return list;
	}

	@Override
	public List<Article> findByType(Integer type, Integer size) {
		return articleMapper.selectByType(type, size);
	}

}
